<?php

namespace search\controllers\bigdata;

use business\elasticsearch\services\BigDataTakenLandService;
use business\elasticsearch\services\BigDataWinBidService;

class WinBidController extends BaseController
{
    public $enableCsrfValidation = false;//csrf安全

    /**
     * @api {get} /bigdata/win-bid/get-list 01、大数据中标搜索
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataTakenLand
     *
     * @apiDescription 01、大数据中标搜索
     *
     * @apiParam {String} keyword                               搜索词
     * @apiParam {String} province                              省份
     * @apiParam {String} city                                  城市
     * @apiParam {String} company_type                          企业类型
     * @apiParam {String} source_type                           信息类型
     * @apiParam {String} publish_date_lower/publish_date_upper 发布时间
     * @apiParam {String} bid_price_lower/bid_price_upper       成交价格
     * @apiParam {String} area                                  省份和城市的混合搜索
     * @apiParam {String} page                                  页码
     * @apiParam {String} pageSize                              分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     *
     */
    public function actionGetList()
    {
        try {
            $keyword = $this->getParam('keyword');
            $province = $this->getParam('province_code');
            $city = $this->getParam('city_code');
            $companyType = $this->getParam('company_type_label');
            $sourceType = $this->getParam('source_type');
            $subSource = $this->getParam('source_sub');
            $publishDateLower = $this->getParam('publish_date_lower');
            $publishDateUpper = $this->getParam('publish_date_upper');
            $bidPriceLower = $this->getParam('bid_price_lower');
            $bidPriceUpper = $this->getParam('bid_price_upper');
            $areaCodes = $this->getParam('area') ? explode(',', trim($this->getParam('area'))) : [];
            $page = $this->getParam('page');
            $pageSize = $this->getParam('page_size');
            $page = intval($page) > 0 ? intval($page) : 1;
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;
            if (!empty($province)) {
                $province = explode(",", $province);
            } else {
                $province = [];
            }
            if (!empty($city)) {
                $city = explode(",", $city);
            } else {
                $city = [];
            }

            $tags = [];
            $tags['developer_top_100_label'] = $this->getParam('developer_top_100_label');
            $tags['new_decorate_expand_label'] = $this->getParam('new_decorate_expand_label');
            $tags['new_land_usage_expand_label'] = $this->getParam('new_land_usage_expand_label');
            $tags['new_area_expand_label'] = $this->getParam('new_area_expand_label');
            $tags['price_gt_avg_label'] = $this->getParam('price_gt_avg_label');

            $bigDataDeveloperService = new BigDataWinBidService();
            $rs = $bigDataDeveloperService->getDataList(
                $keyword,
                $province,
                $city,
                $companyType,
                $sourceType,
                $subSource,
                $publishDateLower,
                $publishDateUpper,
                $bidPriceLower,
                $bidPriceUpper,
                $areaCodes,
                $tags,
                $page,
                $pageSize
            );
            return $this->success($rs);
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }

    /**
     * @api {get} /bigdata/purchase/guess-names 02、土地联想
     *
     * @apiVersion 1.0.0
     * @apiGroup bigdataTakenLand
     *
     * @apiDescription 02、土地联想
     *
     * @apiParam {String} keyword                   搜索词
     * @apiParam {String} pageSize                  分页size
     *
     * @apiSuccess {Boolean} success                    返回状态
     * @apiSuccess {String}  message                    返回消息内容
     * @apiSuccess {Number}  errorCode                  错误码
     * @apiSuccess {Object}  data                       对象数组
     * @apiSuccess {Object}  data.total
     * @apiSuccess {Object}  data.list
     * @apiSuccess {Number}  data.list.titleHL     搜索词高亮土地名
     * @apiSuccess {String}  data.list.title       土地名
     *
     *
     */
    public function actionGuessNames()
    {
        try {
            $keyword = $this->getParam('keyword');
            $pageSize = $this->getParam('page_size');
            $pageSize = intval($pageSize) > 0 ? intval($pageSize) : 10;

            if (empty($keyword)) {
                throw new \Exception('你的关键字没有传');
            }
            $bigDataPurchaseService = new BigDataWinBidService();
            $rs = $bigDataPurchaseService->guessNames($keyword, $pageSize);
            return $this->success($rs);
        } catch (\Exception $ex) {
            \Yii::error($ex->getMessage());
            return $this->error('', '获取失败，请稍后重试');
        }
    }
}
